package com.novellotus.iov.carvice.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.novellotus.httploadingview.LoadingView;
import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.adapter.MessageAdapter;
import com.novellotus.iov.carvice.bean.RxMessageBean;
import com.novellotus.iov.carvice.retrofit.BaseEntity;
import com.novellotus.iov.carvice.retrofit.BaseObserver;
import com.novellotus.iov.carvice.retrofit.RetrofitFactory;
import com.novellotus.iov.carvice.retrofit.RxSchedulers;
import com.novellotus.iov.carvice.utils.MyViewUtils;
import com.novellotus.iov.carvice.utils.SharedPreferencesUtils;
import com.novellotus.iov.carvice.view.PageView;
import com.orangegangsters.github.swipyrefreshlayout.library.RefreshLayout;
import com.orangegangsters.github.swipyrefreshlayout.library.RefreshLayoutDirection;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.schedulers.Schedulers;

/**
 * 消息页面
 */
public class MessageActivity extends BaseActivity implements RefreshLayout.OnRefreshListener, LoadingView.OnBtnClickListener, PageView.PageViewInterface {

    @BindView(R.id.pageView)
    PageView pageView;
    @BindView(R.id.tv_tittle)
    TextView tv_tittle;

    MessageAdapter messageAdapter;
    final int MAX_PAGE = 10;

    @Override
    protected void initContentView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_message);
    }

    @Override
    public void init() {
        tv_tittle.setText("消息中心");
        pageView.setLoadFinish(false);
        pageView.initLoadingView(this);
        pageView.refreshLayout.setOnRefreshListener(this);
        pageView.setPageViewInterface(this);
        pageView.loadingView.mWarnView.setTextColor(getResources().getColor(R.color.text_black));
        getMessageData(pageView.getLoadCurrentPage());

    }


    public void setData(List<RxMessageBean.RecordsBean> recordsBeanList) {
        messageAdapter = new MessageAdapter(recordsBeanList);
        pageView.setBottomInterface(messageAdapter);
        pageView.setRecyclerViewAdapter(MyViewUtils.getLinearLayoutManager(this), messageAdapter);
    }

    /**
     * 获取个人信息
     */
    Observable<BaseEntity<RxMessageBean>> observable;

    public void getMessageData(int currentPate) {
        observable = RetrofitFactory.getInstance().getMessage(SharedPreferencesUtils.getKey(this)[0], currentPate, MAX_PAGE);
        observable.compose(RxSchedulers.<BaseEntity<RxMessageBean>>compose())
                .subscribe(new BaseObserver<RxMessageBean>(this) {
                    @Override
                    public void onSuccess(RxMessageBean response) {

                        if (response.getTotal() == 0) {
                            pageView.loadFail("暂无消息", false);
                        } else {
                            pageView.setLoadFinish(true);
                            pageView.loadingView.success();
                            pageView.setTotalPage(response.getTotal(), MAX_PAGE);
                            if (messageAdapter != null) {
                                if (response.getCurrent() == 1) {
                                    messageAdapter.update(response.getRecords());
                                } else {
                                    messageAdapter.addData(response.getRecords());
                                }
                            } else {
                                setData(response.getRecords());
                            }
                        }

                    }

                    @Override
                    public void onFail(int code, String msg) {
                        pageView.setLoadFinish(true);
                        pageView.loadFail(msg, true);
                        pageView.setVisibility(View.VISIBLE);
                        pageView.loadingView.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onError(String msg) {
                        pageView.setLoadFinish(true);
                        pageView.loadFail(msg, true);
                        pageView.loadingView.setVisibility(View.VISIBLE);
                    }
                });
    }

    @OnClick(R.id.img_back)
    void back() {
        finish();
    }

    @Override
    public void loadData(int dataPage) {
        getMessageData(pageView.currentPage);
    }

    @Override
    public void onReload() {
        pageView.loadingView.loading();
        getMessageData(pageView.currentPage);
    }

    @Override
    public void onRefresh(RefreshLayoutDirection direction) {
        pageView.refreshData(direction);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (observable != null) {
            observable.unsubscribeOn(Schedulers.io());
        }
    }
}
